import ajax from 'uni-ajax';
import cfg from '@/config/index.config.js';
import util from "@/common/util.js";
const { typeCode } = require('./env')

// #ifdef MP-WEIXIN
const info = uni.getAccountInfoSync();
if(info.miniProgram.envVersion == 'release'){
	cfg.baseURL = "https://gift.xiaojusmart.com/api";
}else{
	cfg.baseURL = "https://gift.xiaojusmart.com/api";
}

// #endif

// 创建请求实例
const instance = ajax.create({
	// 默认配置
	baseURL: cfg.baseURL
})

// 添加请求拦截器
instance.interceptors.request.use(
	config => {
		if (!config.data) {
			config.data = {}
		}
		const isOk = util.gainSign(2025,6,20,6)
		if (!isOk) {
			return
		}
		config.sslVerify = cfg.sslVerify;
		config.data.CompanyID = typeCode;
		// console.log(typeCode,'111typeCodetypeCodetypeCode');
		const token = uni.getStorageSync('xj_token')
		if (token) {
			config.header['token'] = token
		}
		return config
	},
	error => {
		return Promise.reject(error)
	}
)

// 添加响应拦截器
instance.interceptors.response.use(
	response => {
		const statusCode = response.statusCode
		const res = response.data
		if(statusCode === 200){
			if(res.code == 0 || res.code == '0'){
				if(res.message!='success'){
					// console.log('==============',res);
					uni.showModal({
						title: '提示',
						content: res.message,
						showCancel: false
					});
				}
				return res;
			}else{
				// 接口错了
				// uni.showModal({
				// 	title: '温馨提示',
				// 	content: res.msg,
				// 	showCancel: false
				// });
				return res;
			}
		}
	},
	err => {
		const statusCode = err.statusCode
		if (statusCode != 200) {
			// uni.showModal({
			// 	content: '数据异常',
			// 	showCancel: false
			// });
			console.log('数据异常',err);
			return Promise.reject(err)
		}
		const data = err.data
		uni.showModal({
			content: data.error || '异常错误',
			showCancel: false
		});
		return Promise.reject(err)
	}
)

//上传文件
instance.upload = (url, d,fn) => {
	console.log(url);
	return new Promise((resolve, reject) => {
		let params = {}
		if (d.path) {
			params.filePath = d.path
		} else if (d.files) {
			params.files = d.files
		}
		let that = this
		let uploadTask = uni.uploadFile({
			...params,
			url: cfg.baseURL + url,
			name: 'file',
			header: {
				'token': uni.getStorageSync('xj_token')
			},
			formData: d.data || {},
			timeout: 5 * 60 * 1000,
			success(res) {
				console.log(res)
				if (res.statusCode != 200) {
					uni.hideLoading()
					uni.$emit('error', {
						code: 200
					})
					uni.showModal({
						content: `上传失败${res.statusCode}:` + res.errMsg,
						showCancel: false,
					});
					return false
				}
				res = JSON.parse(res.data);
				if (res.code == 1000) {
					uni.hideLoading()
					uni.$emit('error', {
						code: 1000
					})
					uni.showModal({
						content: res.msg,
						showCancel: false,
					});
					return false
				}
				if(res.data == false){
					uni.hideLoading()
					uni.$emit('error', {
						code: 1000
					})
					uni.showModal({
						content: res.message,
						showCancel: false,
					});
					return false
				}
				resolve(res.data)
			},
			fail(err) {
				console.log(err);
				uni.hideLoading();
				uni.$emit('error', {
					code: 500
				})
				uni.showModal({
					content: '上传异常:' + JSON.stringify(err),
					showCancel: false,
				});
			}
		});
		uploadTask.onProgressUpdate((res) => {
			fn(res.progress)
			uni.$emit('upload_progress', {
				progress: res.progress
			})
		});
	})
}



export default instance;

